package ejercicios;

import interfaces.tests.CountPowerfulNumbers;

public class CountPowerfulNumbersImpl implements CountPowerfulNumbers {

	@Override
	public int countPowerfulNumbers(int from, int to) {
		int count = 0;
		for (int helper = from; helper <= to; helper++){
			if (isPowerfulNumber(helper)){
				count++;
			}
		}
		return count;
	}
	
	private boolean isPowerfulNumber(int number) {
		for (int index = number/2; index>0; index--){
			if (isPrime(index) && divides(number, index) && !divides(number, index*index)){
				return false;
			}
		}
		return true;
	}

	private boolean divides(int number, int divisor) {
		return number % divisor == 0;
	}
	
	public boolean isPrime(int n) {
	    return !new String(new char[n]).matches(".?|(..+?)\\1+");
	}
}
